package com.shitou.shitouproduct.service.impl;

import com.shitou.shitoucommons.utils.RespBean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.Query;

import com.shitou.shitouproduct.dao.AttrDao;
import com.shitou.shitouproduct.entity.AttrEntity;
import com.shitou.shitouproduct.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }



    @Override
    public RespBean attrByCategoryId(int categoryId) {
        if( categoryId == 0 ){
            return RespBean.error("分类出错");
        }
        List<AttrEntity> listDo = this.list(new QueryWrapper<AttrEntity>()
                .eq("category_id", categoryId)
                .eq("status", 1));

        if ( listDo.size() <= 0 ){
            return RespBean.error("该分类没有属性，可直接售卖商品");
        }

        // 分割数据
        List<AttrEntity> entityList = listDo.stream().map(attrEntity -> {
            attrEntity.setSplitValue(attrEntity.getValueSelect().split(";"));
            return attrEntity;
        }).collect(Collectors.toList());

        return RespBean.success().put("data", entityList);
    }


    /**
     * 通过分类获取
     * @param params
     * @param categoryId
     * @return
     */
    @Override
    public PageUtils baseList(Map<String, Object> params, Long categoryId) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>().eq("category_id", categoryId)
        );

        return new PageUtils(page);
    }

}

